package com.na.tools.search;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.na.tools.CommonTools;
import com.na.tools.HttpUtils;
import com.na.tools.HttpUtils.HttpRequest;

public class HttpMoniter {

	static final Random RND = new Random();
	
	static final List<IpTable> IPTABLES = collectIp();
	static final String URL = "http://www.zcool.com.cn/work/ZMTI5NDEzNzY=.html";
	static final String METHOD = "GET";
//				Connection:keep-alive
//				Cookie:zid=1435821543vwcv; randomcodetoken=6965b6cab2d82b3d4b9552894a61b420; JSESSIONID=abcA7tvSqSKDX-23r4q6u; Hm_lvt_6100b0cf343809acdb5765a0be722a2c=1436750296,1436763275,1436930229,1436949750; Hm_lpvt_6100b0cf343809acdb5765a0be722a2c=1436949804; CNZZDATA30039725=cnzz_eid%3D1761414532-1435817870-%26ntime%3D1436944585; bfd_s=190930977.28380896.1436946615481; tmc=6.190930977.84219169.1436949751052.1436949787816.1436949804279; tma=190930977.37856112.1435821544056.1436750297214.1436930230000.7; tmd=179.190930977.37856112.1435821544056.; bfd_g=8a7bc81f66bd068d00006f280046788b558bc257
	static final String[] USERAGENT = {
			"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36",
			"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
			"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.134 Safari/537.36",
	};
	static final String[] HEADERS = {
			"Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
			"Accept-Encoding", "gzip, deflate, sdch",
			"Accept-Language", "zh-CN,zh;q=0.8",
			"Cache-Control", "max-age=0",
			"Host", "www.zcool.com.cn",
			"User-Agent",
	};
	static final int COUNT = 200;//RND.nextInt(50) + 100;
	
	public static void main(String[] args) throws Exception {
		
		new Thread() {
			@Override
			public void run() {
				int c = 0;
				System.out.println("count:" + COUNT);
				HttpRequest request = new HttpRequest();
				while (c++ < COUNT) {
					request.method = METHOD;
					request.url = URL;
					request.requestHeaders = getHeaders();
					byte[] data;
					try {
						data = HttpUtils.request(request);
						System.out.println(c + ":" + (data != null ? data.length : 0));
					} catch (IOException e) {
					}
					randomSleep();
				}
			}
		}.start();
		
	}
	
	static String[] getHeaders() {
		List<String> ret = new ArrayList<String>(HEADERS.length + 3);
		for (String s : HEADERS) {
			ret.add(s);
		}
		String agent = USERAGENT[RND.nextInt(USERAGENT.length)];
		ret.add(agent);
		randomIp(ret);
		return ret.toArray(new String[ret.size()]);
	}
	
	static void randomIp(List<String> headers) {
		String[] ipHeaders = {
				"x-forwarded-for",
				"Proxy-Client-IP",
				"WL-Proxy-Client-IP"
		};
		String h = ipHeaders[RND.nextInt(ipHeaders.length)];
		headers.add(h);
		IpTable table = IPTABLES.get(RND.nextInt(IPTABLES.size()));
		headers.add(table.randomIp());
	}
	
	static void randomSleep() {
		try {
//			Thread.sleep(RND.nextInt(10000) + 2000);
			synchronized (HttpMoniter.class) {
				HttpMoniter.class.wait(RND.nextInt(10000) + 2000);
			}
		} catch (InterruptedException e) {
		}
	}
	
	static List<IpTable> collectIp() {
		byte[] bytes = CommonTools.readFile(HttpMoniter.class.getResourceAsStream("ip.txt"));
		if (bytes == null) {
			return null;
		}
		String content = new String(bytes);
		String[] ips = content.split(System.lineSeparator());
		List<IpTable> result = new ArrayList<IpTable>(ips.length);
		for (String ip : ips) {
			IpTable table = new IpTable(ip);
			result.add(table);
		}
		return result;
	}
	
}
